<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/public/form::head(_)"/>
<link rel="stylesheet" th:href="@{/js/md/css/editormd.css}">
<script type="text/javascript" th:src="@{/js/md/editormd.js}"></script>
<body th:replace="admin/public/form::body(~{::div})">
<div class="layui-card">
    <div class="layui-card-header ">
        <span class="layui-breadcrumb">
          <a href="/admin" target="_blank">首页</a>
          <a href="javascript:;">资讯管理</a>
          <a href="javascript:;" onclick="parent.xadmin.add_tab('资讯列表','/admin/article/index')">资讯列表</a>
          <a><cite>添加资讯</cite></a>
        </span>
        <a class="layui-btn layui-btn-sm" style="line-height:1.6em;margin-top:3px;float:right"
           href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon layui-icon-refresh-3" style="line-height:30px"></i></a>
    </div>
    <div class="layui-card-body layui-tab">
        <form id="form1" class="layui-form">

            <div class="layui-form-item">
                <label class="layui-form-label">选择分类</label>
                <div class="layui-input-inline" style="width: auto">
                    <select name="categoryId" id="pid-select" lay-verify="required" lay-filter="pid-select"
                            lay-search>
                        <option value="0" data-level="1">请选择分类</option>
                        <option th:each="category:${categories}" th:value="${category.id}" th:text="${category.title}">
                            顶级资讯分类
                        </option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">资讯标题</label>
                <div class="layui-input-block">
                    <input type="text" name="title" lay-verify="required" lay-reqtext="请填写资讯标题！"
                           placeholder="请输入资讯标题" autocomplete="off" class="layui-input ">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">封面图</label>
                <div class="layui-input-inline" style="width: 600px">
                    <input type="text" class="layui-input " id="cover_input" name="cover" lay-verify="required"
                           lay-reqtext="请上传图片">
                </div>
                <div class="layui-input-inline" style="width: 250px">
                    <b class="layui-btn" id="test1">上传封面图</b>
                    <button class="layui-btn layui-btn-normal" id="pick_pic">选择在线图片</button>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">预览</label>
                <div class="layui-input-block">
                    <div class="layui-upload">
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="demo1" style="max-height: 150px">
                            <p id="demoText"></p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">浏览量</label>
                <div class="layui-input-block">
                    <input type="text" name="browse"
                           placeholder="请输入默认浏览量" autocomplete="off" class="layui-input " value="0">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">标签</label>
                <div class="layui-input-block">
                    <div id="tags">
                        <input type="text" id="inputTags" placeholder="回车生成标签" autocomplete="off">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">操作</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="top" value="1" title="置顶">
                    <input type="checkbox" name="hot" value="1" title="热门">
                    <input type="checkbox" name="recommend" value="1" title="推荐">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">摘要</label>
                <div class="layui-input-block">
                    <textarea name="introduction" style="width: 100%;height: 90px"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">资讯内容</label>
                <div class="layui-input-block" id="editormd">
                    <textarea style="display:none;" name="contentMarkdown"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="add">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
<script>
    function setUrl(val){
        $('#cover_input').val(val);$('#demo1').attr('src',val);
    }
    layui.extend({
        inputTags: '/xadmin/lib/layui-v2.5.7/lay/modules/tags/inputTags'
    }).use(['jquery', 'form', 'layer', 'upload', 'inputTags'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            inputTags=layui.inputTags,
            upload = layui.upload;
        //创建标签
        let tags=inputTags.render({
            elem:'#inputTags',
            content: [],
            aldaBtn: false,
            done: function(value){
                console.log(value);//啥破组件，不给获取所有数据的接口
                tags=value;
            }
        });
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            , url: '/admin/upload/img?type=article_cover'
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg(res.msg);
                }
                layer.msg('上传成功！');
                $("input[name='cover']").val(res.data.src)
                //上传成功
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
        var testEditor = editormd("editormd", {
            width: "93%",
            height: 640,
            markdown : "",
            path : '/js/md/lib/',
            imageUpload : true,
            imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
            imageUploadURL : "/admin/upload/upload_img_markdown?type=md",
            saveHTMLToTextarea : true
            /*
             上传的后台只需要返回一个 JSON 数据，结构如下：
             {
                success : 0 | 1,           // 0 表示上传失败，1 表示上传成功
                message : "提示的信息，上传成功或上传失败及错误信息等。",
                url     : "图片地址"        // 上传成功时才返回
             }
             */
        });
        //监听提交
        form.on('submit(add)', function (data) {
            //获取标签
            let send_json = data.field;
            let ems=$('#tags em');
            let tags=[];
            ems.each(function () {
                tags.push(this.innerText);
            });
            //删除空的file
            delete send_json.file;
            send_json.tags=tags;
            send_json.content=testEditor.getHTML();
            delete send_json['editormd-html-code'];
            $.ajax({
                url: '/admin/article/add',
                method: 'post',
                data: send_json,
                dataType: 'JSON',
                success: function (res) {
                    if (res.code == 0) {
                        layer.alert("增加成功", {
                            icon: 6
                        }, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg(res.msg, {icon: 5});
                    }

                },
                error: function (data) {
                    layer.msg('服务器繁忙', {icon: 5});
                }
            });
            return false;
        });
        $('#pick_pic').click(function () {
            var w=($(window).width()*0.9);
            var h=($(window).height() - 50);
            var index = layer.open({
                type: 2,
                area: [w+'px', h +'px'],
                fix: false, //不固定
                maxmin: true,
                shadeClose: true,
                shade:0.4,
                title: '选择图片',
                content: '/admin/pic/index',
                success:function(layero, index){
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']];
                    var js_str='$(\'#cover_input\').val("{0}");$(\'#demo1\').attr(\'src\',"{0}");';
                    iframeWin.js_str=js_str;//看这
                }
            });
            return false
        });

    });
</script>
</html>